package com.orangelife.mobile.goodsOrder.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.common.jump.OrderJumper;
import com.common.util.MoneyUtil2;
import com.curry.android.adapter.FatherAdapter;
import com.curry.android.util.StringUtil;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.goodsOrder.biz.OrderBiz;
import com.orangelife.mobile.widget.ScrollListView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class GoodsOrderAdapter<T> extends FatherAdapter<T> {
    private Context context;
    private List<Map<String, Object>> list;
    private List<Map<String, Object>> goods;
    private boolean showDate = false;
    public jumpInterface jump;

    public void showDate(boolean showDate) {
        this.showDate = showDate;
    }

    public GoodsOrderAdapter(Context context,
                             List<Map<String, Object>> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressWarnings("unchecked")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.layout_order_item, null);
            viewHolder.btnDeliver = (Button) convertView.findViewById(R.id.btnDeliver);
            viewHolder.tvCommName = (TextView) convertView.findViewById(R.id.tvCommName);
            viewHolder.tvTotalMoney = (TextView) convertView.findViewById(R.id.tvTotalMoney);
            viewHolder.tvStatus = (TextView) convertView.findViewById(R.id.tvStatus);
            viewHolder.tvRefundMoney = (TextView) convertView.findViewById(R.id.tvRefundMoney);
            viewHolder.slvGoods = (ScrollListView) convertView.findViewById(R.id.slvGoods);
            viewHolder.llOrderDate = (LinearLayout) convertView.findViewById(R.id.llOrderDate);
            viewHolder.tvOrderDate = (TextView) convertView.findViewById(R.id.tvOrderDate);
            viewHolder.llMyGoods = (LinearLayout) convertView
                    .findViewById(R.id.llMyGoodsItem);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        Map<String, Object> map = list.get(position);
        String viewType = String.valueOf(map.get("viewType"));// 1 金额 2 积分
        String totalCost = String.valueOf(list.get(position).get("totalCost"));
        if ("2".equals(viewType)) {
            totalCost = MoneyUtil2.trimZero(totalCost) + "ml橙汁";
        } else {
            totalCost = Constant.RMB_FLAG + totalCost;
        }
        viewHolder.tvTotalMoney.setText("订单总额：" + totalCost);

        String orderStatus = String.valueOf(map.get("orderStatus1"));

        String refundMoney = String.valueOf(map.get("refundMoney"));

        viewHolder.tvStatus.setText(orderStatus);
        int status = -1;
        try {
            status = Integer.parseInt(String.valueOf(map.get("orderStatus")));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }

        //'订单状态(0待付款；1待卖家发货；2卖家已发货；3交易完成;4已取消；5支付中;6取消订单中；7受理中；
        // 8已退款;9拒绝取消;10拒绝退款;11同意退款;12待卖家收货(即买家退回商品);13卖家已收货)'
        viewHolder.tvTotalMoney.setTextColor(context.getResources().getColor(R.color.lightOrange));
        Integer[] num = {4, 5, 6, 7, 8, 9, 12, 13};
        if (status == 0) {
            viewHolder.tvRefundMoney.setVisibility(View.GONE);
            if ("已超时".equals(orderStatus)) {
                viewHolder.btnDeliver.setVisibility(View.INVISIBLE);
            } else {
                viewHolder.btnDeliver.setVisibility(View.VISIBLE);
            }
            viewHolder.btnDeliver.setText("立即支付");
        } else if (status == 2 || status == 10) {
            viewHolder.tvRefundMoney.setVisibility(View.GONE);
            viewHolder.btnDeliver.setVisibility(View.VISIBLE);
            viewHolder.btnDeliver.setText("确认收货");
        } else if (status == 1 || status == 3) {
            viewHolder.tvRefundMoney.setVisibility(View.GONE);
            viewHolder.btnDeliver.setVisibility(View.GONE);
        } else if (Arrays.asList(num).contains(status)) {
            viewHolder.btnDeliver.setVisibility(View.GONE);
            if (StringUtil.isBlank(refundMoney)) {
                viewHolder.tvRefundMoney.setVisibility(View.GONE);
            } else {
                viewHolder.tvRefundMoney.setVisibility(View.VISIBLE);
                viewHolder.tvTotalMoney.setTextColor(context.getResources().getColor(R.color.text_grey));
                viewHolder.tvRefundMoney.setText("退款金额：¥" + refundMoney);
            }
        } else if (status == 11) {
            viewHolder.tvRefundMoney.setVisibility(View.VISIBLE);
            viewHolder.btnDeliver.setVisibility(View.VISIBLE);
            viewHolder.tvTotalMoney.setTextColor(context.getResources().getColor(R.color.text_grey));
            viewHolder.tvRefundMoney.setText("退款金额：¥" + refundMoney);
            viewHolder.btnDeliver.setText("返回商品");
        }

        viewHolder.tvCommName.setText(String.valueOf(map.get("merchantName")));
        int readFlag = Integer.parseInt(String.valueOf(map.get("readFlag")));//'阅读标记(0未阅读，1已阅读)'
        if (readFlag == 0) {
            viewHolder.tvCommName.setTextColor(context.getResources().getColor(R.color.result_view));
            viewHolder.tvStatus.getPaint().setFakeBoldText(true);
            viewHolder.tvCommName.getPaint().setFakeBoldText(true);
        } else {
            viewHolder.tvCommName.setTextColor(context.getResources().getColor(R.color.text_black));
            viewHolder.tvStatus.getPaint().setFakeBoldText(false);
            viewHolder.tvCommName.getPaint().setFakeBoldText(false);
        }


        viewHolder.btnDeliver.setTag(R.id.btnDeliver, status);
        viewHolder.btnDeliver.setTag(R.id.tvOrderDate, map);
        viewHolder.btnDeliver.setOnClickListener(onClickListener);

        viewHolder.llMyGoods.setTag(R.id.slvGoods, map.get("orderID"));
        viewHolder.llMyGoods.setTag(R.id.llMyGoodsItem, map.get("orderType"));
        viewHolder.llMyGoods.setOnClickListener(onClickListener);

        //订单商品 ↓
        goods = (List<Map<String, Object>>) map.get("goods");
        if (goods == null) {
            goods = new ArrayList<>();
        }
        GoodsOrderListChildrenAdapter<Map<String, Object>> adapter = new GoodsOrderListChildrenAdapter<>(context, goods);
        adapter.setViewType(viewType);
        viewHolder.slvGoods.setAdapter(adapter);

        viewHolder.slvGoods.setTag(R.id.slvGoods, map.get("orderID"));
        viewHolder.slvGoods.setTag(R.id.llMyGoodsItem, map.get("orderType"));
        viewHolder.slvGoods.setOnItemClickListener(onItemClickListener);

        showOrderDate(viewHolder, position, String.valueOf(map.get("tscreate")));
        return convertView;

    }


    private void showOrderDate(ViewHolder holder, int position, String date) {
        if (showDate) {/*只有全部订单才显示时间，但是搜索也是属于全部订单，但不显示*/
            String preDate = "0";
            if (position > 0) {
                preDate = list.get(position - 1).get("tscreate").toString();
            }
            String preData1 = OrderBiz.formatDateTime(preDate);
            String date1 = OrderBiz.formatDateTime(date);
            if (!preData1.equals(date1)) {/*如果和前一项的时间不相同，则显示*/
                holder.llOrderDate.setVisibility(View.VISIBLE);
                holder.tvOrderDate.setText(date1);
            } else {
                holder.llOrderDate.setVisibility(View.GONE);
            }
        } else {
            holder.llOrderDate.setVisibility(View.GONE);
        }
    }

    AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String orderID = (String) parent.getTag(R.id.slvGoods);
            String orderType = (String) parent.getTag(R.id.llMyGoodsItem);
            jumpToGoodOrderDetail(orderID, orderType);
        }
    };

    private void jumpToGoodOrderDetail(String orderID, String orderType) {
        OrderJumper.jumpToGoodOrderDetail((Activity) context, orderID, orderType);
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.llMyGoodsItem:
                    String orderID = (String) v.getTag(R.id.slvGoods);
                    String orderType = (String) v.getTag(R.id.llMyGoodsItem);
                    jumpToGoodOrderDetail(orderID, orderType);
                    break;
                case R.id.btnDeliver:
                    int status = (int) v.getTag(R.id.btnDeliver);
                    jump(status, (Map<String, Object>) v.getTag(R.id.tvOrderDate));
                    break;
            }
        }
    };

    private void jump(int status, Map<String, Object> map) {
        switch (status) {
            case 0:
                jump.pay(map);
                break;
            case 2:
                String orderID = String.valueOf(map.get("orderID"));
                jump.confirm(orderID);
                break;
            case 10:
                String orderID2 = String.valueOf(map.get("orderID"));
                jump.confirm(orderID2);
                break;
            case 11:
                String applicationID = String.valueOf(map.get("applicationID"));//退款申请ID
                jump.backGood(applicationID);
                break;
            default:
                break;
        }
    }

    public void setJump(jumpInterface jump) {
        this.jump = jump;
    }

    public interface jumpInterface {
        void pay(Map<String, Object> map);

        void confirm(String orderID);

        void backGood(String applicationID);
    }

    class ViewHolder {
        private LinearLayout llOrderDate;
        private TextView tvOrderDate;
        private Button btnDeliver;
        private TextView tvCommName;
        private TextView tvTotalMoney;
        private TextView tvStatus;
        private TextView tvRefundMoney;
        private ScrollListView slvGoods;
        private LinearLayout llMyGoods;
    }

    @Override
    public BaseAdapter setList(List<T> list) {
        this.list = (List<Map<String, Object>>) list;
        return GoodsOrderAdapter.this;
    }

}
